package controller.v2;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;

import model.LogTicketVO;
import model.MemberVO;
import service.v2.ConfigService;
import service.v2.LogTicketService;
import service.v2.MemberService;
import util.PackageUtil;

@RequestMapping("/v2/logticket")
@Controller
public class LogTicketController {
	@Autowired
	private LogTicketService logTicketService;
	@Autowired
	private ConfigService configService;
	@Autowired
	private MemberService memberService;
	
	/**
	 * 兑换券明细
	 * @param token
	 * @param member_id
	 * @param bill_type(1,5:支出 2,3：收入)
	 * @param transaction_type(1充值2消费3销售4过期流转5退款)
	 * @param days
	 * @param page_index
	 * 
	 */
	@RequestMapping(value="/ticketsLog",method=RequestMethod.GET)
	public String ticketsLog(@RequestHeader(value = "LANG", required = false) String lang,@RequestHeader (value="OS", required=false) String os,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, HttpServletResponse response,
				MemberVO memberVO,LogTicketVO logTicketVO,String isJSON)throws Exception{
		if(os != null){
			os = os.toLowerCase();
		}
		String host = configService.loadConfig("HOST");
		String version=configService.loadConfig("VERSION");
		String ticket_name=configService.loadConfig("TICKET_NAME");

		logTicketVO.setMember_id(member_id);
		logTicketVO.setLang(lang);
		int total = logTicketService.getTotal(logTicketVO);
		logTicketVO.setPagination(logTicketVO.getPage_index(),total+"");
		List<LogTicketVO> ticketLogList=logTicketService.selectTicketLogList(logTicketVO);
		
		if(!"1".equals(isJSON)){
			request.setAttribute("ticketLogList", ticketLogList);
			request.setAttribute("logTicketVO", logTicketVO); 
			request.setAttribute("token", token);
			request.setAttribute("memberId", member_id);
			request.setAttribute("os",os);
			request.setAttribute("ticketslogone_url",host+version+"/logticket/ticketsLogOne");
			//request.setAttribute("ticketslogone_url","http://10.0.0.68:8080/v2/logticket/ticketsLogOne");
			
			//兑换券名称从数据库查出来。
			request.setAttribute("ticket_name",ticket_name);
			request.setAttribute("host_url",host);
		}else{
			LogTicketVO pagination = logTicketService.packPaginationInfo(logTicketVO);

			Map map  =  new HashMap();
			
			map.put("ticketLogList", ticketLogList);
			map.put("pagination", pagination);
			map.put("logTicketVO", logTicketVO); 
			map.put("token", token);
			map.put("memberId", member_id);
			map.put("os",os);
			map.put("ticketslogone_url",host+version+"/logticket/ticketsLogOne");
			
			//兑换券名称从数据库查出来。
			map.put("ticket_name",ticket_name);
			map.put("host_url",host);
			
		    response.setContentType("text/json;charset=utf-8");
	        PrintWriter out = response.getWriter();
	        out.print(JSON.toJSONString(PackageUtil.packageParams(200, "success", map, "")));
	        out.flush();
		    return null;
		}
		return "/app/ticket/ticketsLog"; 
	}
	
	
	/**
	 * 兑换券明细(收入/支出)
	 * @param token
	 * @param member_id
	 * @param bill_type(1,5:支出 2,3：收入)
	 * @param transaction_type(1充值2消费3销售4过期流转5退款)
	 * @param days
	 * @param page_index
	 * 
	 */
	@RequestMapping(value="/ticketsItem",method=RequestMethod.GET)
	public String ticketsItem(@RequestHeader(value = "LANG", required = false) String lang,@RequestHeader (value="OS", required=false) String os,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, HttpServletResponse response,
				MemberVO memberVO,LogTicketVO logTicketVO,String isJSON)throws Exception{
		
		String host = configService.loadConfig("HOST");
		String version=configService.loadConfig("VERSION");
		
		logTicketVO.setMember_id(member_id);
		logTicketVO.setLang(lang);
		
		int total = logTicketService.getTotal(logTicketVO);
		logTicketVO.setPagination(logTicketVO.getPage_index(),total+"");
		List<LogTicketVO> ticketLogList=logTicketService.selectTicketLogList(logTicketVO);
		
		if(!"1".equals(isJSON)){
			request.setAttribute("ticketslogone_url",host+version+"/logticket/ticketsLogOne");
			//request.setAttribute("ticketslogone_url","http://10.0.0.68:8080/v2/logticket/ticketsLogOne");

			request.setAttribute("ticketLogList", ticketLogList);
			request.setAttribute("logTicketVO", logTicketVO); 
			request.setAttribute("token", token);
			request.setAttribute("os", os);
			request.setAttribute("memberId", member_id);
		}else{
			Map map  =  new HashMap();
			LogTicketVO pagination = logTicketService.packPaginationInfo(logTicketVO);

			map.put("ticketslogone_url",host+version+"/logticket/ticketsLogOne");
			
			map.put("ticketLogList", ticketLogList);
			map.put("pagination", pagination);
			map.put("logTicketVO", logTicketVO); 
			map.put("token", token);
			map.put("os", os);
			map.put("memberId", member_id);
			
			response.setContentType("text/json;charset=utf-8");
	        PrintWriter out = response.getWriter();
	        out.print(JSON.toJSONString(PackageUtil.packageParams(200, "success", map, "")));
	        out.flush();
		    return null;
		}
		return "/app/ticket/ticketsItem"; 
	}
	
	
	/**
	 * 交易明细
	 * @param token
	 * @param member_id
	 * @param logticket_id (兑换券明细id)
	 * 
	 */
	@RequestMapping(value="/ticketsLogOne",method=RequestMethod.GET)
	public Object ticketsLogOne(@RequestHeader(value = "LANG", required = false) String lang,@RequestHeader (value="OS", required=false) String os,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, HttpServletResponse response,
			MemberVO memberVO,String logticket_id,LogTicketVO logTicketVO)throws Exception{
		if(os != null){
			os = os.toLowerCase();
		}
		if(logticket_id==null || "".equals(logticket_id)){
			response.setContentType("text/json;charset=utf-8");   
			PrintWriter out = response.getWriter(); 
			out.print(JSON.toJSONString(PackageUtil.packageParams(10001,"缺少必选参数，请参考API文档","","")) );
			out.flush();
			out.close();
			return null;
		}
		//判断状态(有无数据)
		int result = logTicketService.seleckCheckResult(logticket_id);
		if(result ==0){
			response.setContentType("text/json;charset=utf-8");   
			PrintWriter out = response.getWriter(); 
			out.print(JSON.toJSONString(PackageUtil.packageParams(10000,"参数错误，请参考API文档","","")) );
			out.flush();
			out.close();
			return null;
		}
		String host = configService.loadConfig("HOST");
		String version=configService.loadConfig("VERSION");
		
		memberVO = memberService.selectmMemberTable(member_id);
		LogTicketVO	oneLogTicket = logTicketService.selectOneLogTicket(logticket_id,member_id);
		if(lang ==null){
			request.setAttribute("oneLogTicket", oneLogTicket);
			request.setAttribute("memberInfo", memberVO);
			//兑换券名称从数据库查出来。
			request.setAttribute("ticket_name",configService.loadConfig("TICKET_NAME"));
			request.setAttribute("host_url",host);
			request.setAttribute("shopping_money_name",configService.loadConfig("SHOPPING_MONEY_NAME"));
			request.setAttribute("os", os);
			request.setAttribute("ticketslogone_url",host+version+"/logticket/ticketsLogOne");
			//request.setAttribute("ticketslogone_url","http://10.0.0.68:8080/v2/logticket/ticketsLogOne");
			request.setAttribute("wechat_host",configService.loadConfig("WECHAT_HOST"));

			return "/app/ticket/ticketsLogDetail"; 
		}else{
			Map map  =  new HashMap();
			 map.put("oneLogTicket", oneLogTicket);
			 map.put("memberInfo", memberVO);
			 map.put("ticket_name",configService.loadConfig("TICKET_NAME"));
			 map.put("host_url",host);
			 map.put("shopping_money_name",configService.loadConfig("SHOPPING_MONEY_NAME"));
			 map.put("os", os);
			 map.put("ticketslogone_url",host+version+"/logticket/ticketsLogOne");
			 map.put("wechat_host",configService.loadConfig("WECHAT_HOST"));
			 
		    response.setContentType("text/json;charset=utf-8");
	        PrintWriter out = response.getWriter();
	        out.print(JSON.toJSONString(PackageUtil.packageParams(200, "success", map, "")));
	        out.flush();
	        out.close();
	      return null;	
		}
		
	}

	/**
	 * 兑换券明细筛选
	 * @param token
	 * @param member_id
	 * @param bill_type(1,5:支出 2,3：收入)
	 * @param transaction_type(1充值2消费3销售4过期流转5退款)
	 * @param days
	 * @param page_index
	 * 
	 */
	@RequestMapping(value="/ticketsList",method=RequestMethod.GET)
	public String ticketsList(@RequestHeader(value = "LANG", required = false) String lang,@RequestHeader (value="OS", required=false) String os,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,HttpServletRequest request, HttpServletResponse response,
				MemberVO memberVO,LogTicketVO logTicketVO,String isJSON)throws Exception{
		
		String host = configService.loadConfig("HOST");
		String version=configService.loadConfig("VERSION");
		String ticket_name=configService.loadConfig("TICKET_NAME");
		
		logTicketVO.setMember_id(member_id);
		logTicketVO.setLang(lang);
		
		int total = logTicketService.getTotal(logTicketVO);
		logTicketVO.setPagination(logTicketVO.getPage_index(),total+"");
		List<LogTicketVO> ticketLogList=logTicketService.selectTicketLogList(logTicketVO);
		
		if(!"1".equals(isJSON)){
			request.setAttribute("ticketLogList", ticketLogList);
			request.setAttribute("logTicketVO", logTicketVO);
			request.setAttribute("token", token);
			request.setAttribute("os", os);
			request.setAttribute("memberId", member_id);
			request.setAttribute("ticketslogone_url",host+version+"/logticket/ticketsLogOne");
			//request.setAttribute("ticketslogone_url","http://10.0.0.68:8080/v2/logticket/ticketsLogOne");

			//兑换券名称从数据库查出来。
			request.setAttribute("ticket_name",ticket_name);
		}else{
			LogTicketVO pagination = logTicketService.packPaginationInfo(logTicketVO);

			Map map  =  new HashMap();
			
			map.put("ticketLogList", ticketLogList);
			map.put("pagination", pagination);
			map.put("logTicketVO", logTicketVO);
			map.put("token", token);
			map.put("os", os);
			map.put("memberId", member_id);
			map.put("ticketslogone_url",host+version+"/logticket/ticketsLogOne");
			
			//兑换券名称从数据库查出来。
			map.put("ticket_name",ticket_name);
			
			response.setContentType("text/json;charset=utf-8");
	        PrintWriter out = response.getWriter();
	        out.print(JSON.toJSONString(PackageUtil.packageParams(200, "success", map, "")));
	        out.flush();
		    return null;
		}
		
		return "/app/ticket/ticketsList"; 
	}
	
}
